﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Data.SqlClient;
using System.Data;

namespace Department.App_Code
{
    public class EduMaterial
    {
        private string _type = string.Empty;
        private ArrayList _materials = new ArrayList();
        private string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["DepartmentConnectionString"].ConnectionString;
        private SqlCommand cmd = null;
        private int _depId;
        private int _id;

        public int DepId
        {
            get { return _depId; }
            set { _depId = value; }
        }

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public string Type
        {
            get { return _type; }
            set { _type = value; }
        }

        public ArrayList Materials
        {
            get { return _materials; }
            set { _materials = value; }
        }

        public EduMaterial(string type) { _type = type; }

        public static ArrayList GetMaterials()
        {
            ArrayList eduMaterials = new ArrayList();
            for (int i = 0; i < 10; i++)
            {
                EduMaterial edu = new EduMaterial("item" + i.ToString());
                for (int j = 0; j < 5; j++)
                {
                    EduMaterial subItem = new EduMaterial("subitem" + j.ToString());
                    edu.Materials.Add(subItem);
                }
                eduMaterials.Add(edu);
            }
            return eduMaterials;
        }

        private ArrayList GetTypes()
        {
            ArrayList types = new ArrayList();

            SqlConnection conn = new SqlConnection(conStr);  
            DataTable dt = new DataTable();
            try
            {
                conn.Open();
                string sql = @"SELECT DISTINCT EduMaterialType.Name AS emType FROM EduMaterialType INNER JOIN EducationMaterial AS EM 
                               ON EduMaterialType.ID = EM.EduMaterialTypeID INNER JOIN DisciplineFromDB ON EM.DiscID = DisciplineFromDB.DisciplineID INNER JOIN
                               SpecialtiesFromDB AS S ON .DisciplineFromDB.SpecID = S.SpecID WHERE (EM.IsHidden = 0) AND (S.DepartmentID = @DepID) ORDER BY emType";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                cmd.Parameters.AddWithValue("@ID", _id);
                cmd.Parameters.AddWithValue("@DepID", _depId);                
                da.Fill(dt);
                foreach (DataRow row in dt.Rows)
                {
                    types.Add(row["emType"].ToString());
                }

            }
            catch (Exception)
            {}
            finally
            {
                conn.Close();
            }
            return types;
        }

        
    }
}